import Toast from '../../../component/Toast';
import {Modal} from 'antd';
import {routerRedux} from 'dva/router';
import {clone, trim, startWith} from '../../../util/utils';
import keymaster from '../../../util/keymaster';
import {getLoginSession, removeLoginSession} from '../../../plugin/loginSession';
import {removeWebState} from '../../../plugin/webLocalStore';
import {
	addBlog,
    updateBlog,
    getBlogByRecordId,
} from '../service/blogService';

keymaster.banCtrlS();// 禁用原生ctrl + s

const defaultState = {
	currentBlog: undefined,
	blogEdit: false,
	isTop: true,
	isHtmlModel: false,
	catalog: {}
};

export default {

	namespace: 'blogDetail',
	
	state: {...defaultState},
	
	reducers: {
		
		resetState() {
			return {...defaultState};
		},
		
		changeBlogEdit(state, {payload}) {
			return {...state, ...{blogEdit: payload}};
		},
		
		changeIsTop(state, {payload}) {
			return {...state, ...{isTop: payload}};
		},
		
		changeCurrentBlog(state, {payload}) {
			let stateClone = clone(state);
			stateClone.currentBlog = {...stateClone.currentBlog, ...payload};
			return stateClone;
		},
		
		changeIsHtmlModel(state, {isHtml}) {
			return {...state, ...{isHtmlModel: isHtml}};
		},
		
		changeCatalog(state, {catalog}) {
			return {...state, ...{catalog}};
		}
		
	},
	
	effects: {
		
		*init({recordId, editorInit}, {put}) {
			let loginSession = yield getLoginSession();
			let res = yield getBlogByRecordId(recordId);
			if(res && res.code == 1) {
				yield put({type: 'changeCurrentBlog', payload: res.data});
				editorInit(res.data.content);
			} else {
				yield put(routerRedux.push('/'));
				Toast.show('获取日志失败');
			}
		},
		
		*changeCurrentBlogTitle({payload}, {put}) {
			if(payload != undefined) {
				payload = payload.replace(/[\r|\n|\t]/g, '');
				if(payload.length <= 1000) {
					yield put({
						type: 'changeCurrentBlog', 
						payload: {
							title: payload,
						}
					});
				} else {
					Toast.show('标题不能超过1000字');
				}
			}
		},
		
		*changeCurrentBlogContent({payload}, {put}) {
			yield put({
				type: 'changeCurrentBlog', 
				payload: {
					content: payload,
				}
			});
		},
		
		*saveBlog({payload}, {put, select}) {
			let blogState = yield select((state) => state.blogDetail);
			let blogEdit = blogState.blogEdit;
			if(blogEdit) {
				let currentBlog = blogState.currentBlog;
				let recordId = currentBlog.recordId;
				let title = currentBlog.title;
				let content = currentBlog.content;
				if(!title) {
					return Toast.show('标题不能为空');
				}
				let res;
				if(recordId) {
					// 更新
					res = yield updateBlog(recordId, title, content);
				} else {
					// 保存
					res = yield addBlog(title, content);
				}
				if(res.code == 1) {
					yield put({
						type: 'changeCurrentBlog', 
						payload: res.data,
					});
					Toast.show('保存成功');
				} else {
					Toast.show(res.msg);
				}
			}
		},
		
	},
	
	subscriptions: {
		
		reset({dispatch, history}) {
			keymaster.routerBind({
				history: history,
				path: '/blogDetail',
				keyStr: 'ctrl+s',
				callback: () => {
					dispatch({type: 'saveBlog'});
				},
			});
		}
		
	}
	
}